/**
 * 添加或者修改页面
 */
var BasicPrivatecarInfoDlg = {
    data: {
        privatecarFleetId: "",
        privatecarFleetName: "",
        privatecarNumber: "",
        privatecarCarnumber: "",
        privatecarColour: "",
        privatecarVhiclegenus: "",
        privatecarType: "",
        privatecarSureton: "",
        privatecarSurevolume: "",
        privatecarCarlong: "",
        privatecarCarwide: "",
        privatecarCarhigh: "",
        privatecarCarheavy: "",
        GPScode: "",
        cartel: "",
        driverId1: "",
        driver1: "",
        tel1: "",
        driver2: "",
        tel2: "",
        newposition: "",
        newpositiontime: "",
        already: "",
        extremely: "",
        city: "",
        privatecarState: "",
        notes: "",
        version: "",
        createTime: "",
        updateTime: "",
        createUser: "",
        updateUser: ""
    }
};
/**
 * 选择驾驶员
 */
var BasicDriverInfoDlg = {
    data: {
    	zdriver: "",
    	zdrivertel: "",
    }
};

layui.use(['form', 'admin', 'ax', 'laydate', 'upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var upload = layui.upload;
    
    //让当前iframe弹层高度适应
    admin.iframeAuto();

    // 渲染时间选择框--最新位置时间
    laydate.render({
        elem: '#newpositiontime'
    });
    
    // 渲染时间选择框--行驶证发证日期
    laydate.render({
        elem: '#xingshizhengfazhengriqi'
    });
    
    // 渲染时间选择框--行驶证起始日期
    laydate.render({
        elem: '#xingshizhengqishiriqi'
    });
    
    // 渲染时间选择框--行驶证截止日期
    laydate.render({
        elem: '#xingshizhengjiezhiriqi'
    });
    
    // 渲染时间选择框--许可证发证日期
    laydate.render({
        elem: '#xukezhengfazhengriqi'
    });
    
    // 渲染时间选择框--许可证起始日期
    laydate.render({
        elem: '#xukezhengqishiriqi'
    });
    
    // 渲染时间选择框--许可证截止日期
    laydate.render({
        elem: '#xukezhengjiezhiriqi'
    });
    
    // 渲染时间选择框--保险合同发证日期
    laydate.render({
        elem: '#baoxianhetongfazhengriqi'
    });
    
    // 渲染时间选择框--保险合同起始日期
    laydate.render({
        elem: '#baoxianhetongqishiriqi'
    });
    
    // 渲染时间选择框--保险合同截止日期
    laydate.render({
        elem: '#baoxianhetongjiezhiriqi'
    });

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/basicPrivatecar/addItem", function (data) {
            if (data.success){
                Feng.success("Add successful!");
                window.location.href = Feng.ctxPath + '/basicPrivatecar'
            } else {
                Feng.error(data.message)
            }
        }, function (data) {
            Feng.error("Add failed!" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });
    
    upload.render({
        elem: '#imgHead'
        , url: Feng.ctxPath + '/basicDriver/upload'
        , before: function (obj) {
            obj.preview(function (index, file, result) {
                $('#avatarPreview').attr('src', result);
            });
        }
        , done: function (res) {
            $('#xingshizhengfileid').val(res.data.fileId);
        }
        , error: function () {
            Feng.error("Upload failed!");
        }
    });

    //输入车长时
    $("#privatecarCarlong").keyup(function () {
        var privatecarCarlong = $("#privatecarCarlong").val();//长
        var privatecarCarwide = $("#privatecarCarwide").val();//宽
        var privatecarCarhigh = $("#privatecarCarhigh").val();//高
		if(!isNaN(privatecarCarlong) && privatecarCarlong != "" && parseFloat(privatecarCarlong) > 0){
			if(!isNaN(privatecarCarwide) && privatecarCarwide != "" && parseFloat(privatecarCarwide) > 0){
				if(!isNaN(privatecarCarhigh) && privatecarCarhigh != "" && parseFloat(privatecarCarhigh) > 0){
	        		$("#privatecarSurevolume").val(privatecarCarlong * privatecarCarwide * privatecarCarhigh).toFixed(2);//改变准载体积
				}
			}
		}
    });

    //输入车宽时
    $("#privatecarCarwide").keyup(function () {
        var privatecarCarwide = $("#privatecarCarwide").val();//宽
        var privatecarCarlong = $("#privatecarCarlong").val();//长
        var privatecarCarhigh = $("#privatecarCarhigh").val();//高
		if(!isNaN(privatecarCarwide) && privatecarCarwide != "" && parseFloat(privatecarCarwide) > 0){
			if(!isNaN(privatecarCarlong) && privatecarCarlong != "" && parseFloat(privatecarCarlong) > 0){
				if(!isNaN(privatecarCarhigh) && privatecarCarhigh != "" && parseFloat(privatecarCarhigh) > 0){
	        		$("#privatecarSurevolume").val(privatecarCarlong * privatecarCarwide * privatecarCarhigh).toFixed(2);//改变准载体积
				}
			}
		}
    });

    //输入车高时
    $("#privatecarCarhigh").keyup(function () {
        var privatecarCarhigh = $("#privatecarCarhigh").val();//高
        var privatecarCarwide = $("#privatecarCarwide").val();//宽
        var privatecarCarlong = $("#privatecarCarlong").val();//长
		if(!isNaN(privatecarCarhigh) && privatecarCarhigh != "" && parseFloat(privatecarCarhigh) > 0){
			if(!isNaN(privatecarCarwide) && privatecarCarwide != "" && parseFloat(privatecarCarwide) > 0){
				if(!isNaN(privatecarCarlong) && privatecarCarlong != "" && parseFloat(privatecarCarlong) > 0){
	        		$("#privatecarSurevolume").val(privatecarCarlong * privatecarCarwide * privatecarCarhigh).toFixed(2);//改变准载体积
				}
			}
		}
    });
    
    //点击主驾驶员
    $("#driver1").click(function(){
        layer.open({
            type: 2,
            title: 'Select Primary Driver',
            area: ['900px', '600px'],
            content: Feng.ctxPath + '/basicPrivatecar/toDriver',
            end: function () {
                $("#driverId1").val(BasicDriverInfoDlg.data.driverId1);//主司机
                $("#driver1").val(BasicDriverInfoDlg.data.zdriver);//主司机
                $("#tel1").val(BasicDriverInfoDlg.data.zdrivertel);//主司机联系方式
            }
        });
    });
    
    //点击副驾驶员
    $("#driver2").click(function(){
        layer.open({
            type: 2,
            title: 'Select Primary Driver',
            area: ['900px', '600px'],
            content: Feng.ctxPath + '/basicPrivatecar/toDriver',
            end: function () {
                $("#driver2").val(BasicDriverInfoDlg.data.zdriver);//主司机
                $("#tel2").val(BasicDriverInfoDlg.data.zdrivertel);//主司机联系方式
            }
        });
    });
    
    form.on('select(settlement)',function(){
		var settlement = $("#settlement").val();
		if(settlement == "其他"){
			$("#jsdiv1").show();
			$("#jsdiv2").show();
			
			$("#payee").val("");
			$("#bank").val("");
			$("#bankAddress").val("");
			$("#bankAccount").val("");
			$("#bankNo").val("");
			$("#taxRate").val("");
			$("#oilCardCumber").val("");
		} else {
			$("#jsdiv1").hide();
			$("#jsdiv2").hide();
			
			$("#payee").val("");
			$("#bank").val("");
			$("#bankAddress").val("");
			$("#bankAccount").val("");
			$("#bankNo").val("");
			$("#taxRate").val("");
			$("#oilCardCumber").val("");
		}
    });

    //查询库区
    form.on('select(operateId)',function(){
        $("#privatecarFleetId").empty();
        $.ajax({
            url:Feng.ctxPath + '/basicCarrier/Querylist',
            dataType:"json",   //返回格式为json
            async: false,
            data:{"operateId":$("#operateId").val()},    //参数值
            type:"post",   //请求方式
            success:function(data){
                if(data.length>0){
                    var str = "<option value=''>Select Carrier</option>";
                    $.each(data,function(key,value){
                        str += "<option value='"+value.id+"'>"+value.carrierName+"</option>";
                    });
                    $("#privatecarFleetId").html(str);
                }
            }
        });
        form.render('select');
    });

    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/basicPrivatecar'
    });

});
layui.use(['ax', 'upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var upload = layui.upload;
    
    upload.render({
        elem: '#imgHead2'
        , url: Feng.ctxPath + '/basicDriver/upload'
        , before: function (obj) {
            obj.preview(function (index, file, result) {
                $('#avatarPreview2').attr('src', result);
            });
        }
        , done: function (res) {
            $('#xukezhengfileid').val(res.data.fileId);
        }
        , error: function () {
            Feng.error("Upload failed!");
        }
    });
});
layui.use(['ax', 'upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var upload = layui.upload;
    
    upload.render({
        elem: '#imgHead3'
        , url: Feng.ctxPath + '/basicDriver/upload'
        , before: function (obj) {
            obj.preview(function (index, file, result) {
                $('#avatarPreview3').attr('src', result);
            });
        }
        , done: function (res) {
            $('#baoxianhetongfileid').val(res.data.fileId);
        }
        , error: function () {
            Feng.error("Upload failed!");
        }
    });
});
layui.use(['ax', 'upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var upload = layui.upload;
    
    upload.render({
        elem: '#imgHead4'
        , url: Feng.ctxPath + '/basicDriver/upload'
        , before: function (obj) {
            obj.preview(function (index, file, result) {
                $('#avatarPreview4').attr('src', result);
            });
        }
        , done: function (res) {
            $('#carphotofileid').val(res.data.fileId);
        }
        , error: function () {
            Feng.error("Upload failed!");
        }
    });
});